package com.skateboard.reelviewtest.path;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

/**
 * @作者： xuze
 * @时间： 2020/11/24 8:35 PM
 * @描述：
 * 问题：1.位置不好卡 ；
 *      2.后期 数值应以成dp 然后 dp2px()
 */
public  class BigCardLightBorder2View extends View {
    /**
     * 329dp
     * 524dp
     * @param context
     */
    private float spaceWidth=400f;

    private Path leftPath=new Path();

    private Path rightPath=new Path();

    private Paint paint=new Paint();

    private float width;

    private float height;

    private float centerX;

    private float centerY;

    private float radius=74f;

    private float marginGap=5f;

    // 默认的动效周期 2s
    private int defaultDuration = 3000;

    private float paintWidth=10f;

    private float leftStartD,leftStopD;

    private float rightStartD,rightStopD;

    private float pading = 0;

    private int paintAlpha = 255;             // 画笔透明度

    private int maxLength=300;

    // 控制过程的动画
    private ValueAnimator startAnimator;
    private ValueAnimator endAnimator;




    // 测量Path 并截取部分的工具
    private PathMeasure mMeasureLeft;

    private PathMeasure mMeasureRight;

    LinearGradient gradient;

    public BigCardLightBorder2View(Context context) {
        this(context,null);
    }

    public BigCardLightBorder2View(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BigCardLightBorder2View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        initValue();
        initPaint();
        initAnimator();
    }

    private void initValue(){
        paintWidth= dp2px(4);
        pading=paintWidth/2;
    }

    private void initPaint(){
        // 关闭硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        paint.setMaskFilter(new BlurMaskFilter(dp2px(10), BlurMaskFilter.Blur.SOLID));
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(paintWidth);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setAlpha(paintAlpha);

    }

    private void initPath(){
        leftPath.reset();
        leftPath.moveTo(centerX-spaceWidth/2,pading);
        leftPath.lineTo(radius+marginGap,pading);

        leftPath.quadTo(radius+marginGap,radius+pading,marginGap,radius+pading);

        leftPath.lineTo(marginGap,height-pading-radius);

        leftPath.quadTo(marginGap+radius,height-pading-radius,marginGap+radius,height-pading);

        leftPath.lineTo(centerX,height-pading);

        mMeasureLeft.setPath(leftPath, false);

        rightPath.reset();
        rightPath.moveTo(centerX+spaceWidth/2,pading);
        rightPath.lineTo(width-radius-pading,pading);

        rightPath.quadTo(width-radius-pading,pading+radius,width-pading,pading+radius);


        rightPath.lineTo(width-pading,height-radius-pading);

        rightPath.quadTo(width-radius-pading,height-radius-pading,width-radius-pading,height-pading);

        rightPath.lineTo(centerX,height-pading);

        mMeasureRight.setPath(rightPath, false);

        gradient = new LinearGradient(centerX-spaceWidth/2,pading,
                marginGap,height-pading-radius, Color.RED, Color.GREEN, Shader.TileMode.CLAMP);

        paint.setShader(gradient);
    }

    private void initAnimator(){
        startAnimator = ValueAnimator.ofFloat(0, 1).setDuration(defaultDuration);
        startAnimator.setInterpolator(new LinearInterpolator());

        startAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float mAnimatorValue = (float) animation.getAnimatedValue();
                leftStopD=mMeasureLeft.getLength() * mAnimatorValue;
                if(leftStopD<maxLength){
                    leftStartD=0;
                }else{
                    leftStartD=leftStopD-maxLength;
                }
                rightStopD=mMeasureRight.getLength()*mAnimatorValue;
                if(rightStopD<maxLength){
                    rightStartD=0;
                }else{
                    rightStartD=rightStopD-maxLength;
                }
                invalidate();
            }
        });
        startAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                endAnimator.start();
            }
        });
        endAnimator=ValueAnimator.ofFloat(0, 1).setDuration(defaultDuration);
        endAnimator.setInterpolator(new LinearInterpolator());
        endAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                float mAnimatorValue = (float) animation.getAnimatedValue();
                float tempLength=mMeasureLeft.getLength()*mAnimatorValue;
                if(tempLength>=maxLength){
                     endAnimator.cancel();
                }

                leftStartD=mMeasureLeft.getLength()-(maxLength-tempLength);
                leftStopD=mMeasureLeft.getLength();


                rightStartD=mMeasureRight.getLength()-(maxLength-tempLength);
                rightStopD=mMeasureRight.getLength();
                invalidate();
            }
        });



        mMeasureLeft = new PathMeasure();
        mMeasureRight = new PathMeasure();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width=w;
        height=h;
        centerX=w/2;
        centerY=h/2;
        initPath();
        //initPath2();
        Log.e("sssss","=====onSizeChanged======centerX:"+centerX+":====centerY:"+centerY);

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.e("sssss","=====onLayout======left:"+left+":====right:"+right);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));


        Path dst = new Path();
        mMeasureLeft.getSegment(leftStartD,leftStopD, dst, true);
        canvas.drawPath(dst, paint);


        Path dstRight = new Path();
        mMeasureRight.getSegment(rightStartD,rightStopD, dstRight, true);
        canvas.drawPath(dstRight, paint);

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

    }

    public void startAnimation(){
        startAnimator.start();
    }
    private float dp2px(float dpValue) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getResources().getDisplayMetrics());
    }
}
